/**
 * Created by Administrator on 2018/5/4.
 */
define(function (require,exports,module) {
    require("../../../lib/fullcalendar/fullcalendar.min.css");
    require("../../../lib/fullcalendar/scheduler.min.css");
    require("./scheduleTable.css")
    var sTpl = require("./template.html");
    var VueComponent = Vue.extend({
        props:['parentData'],
        template: sTpl,
        methods:{
            initQueryParam:function () {
                var mo =  moment();
                mo.week(mo.week()-1)
                var startDate =mo.format("YYYY-MM-DD");
                var mo2 =  moment();
                var endDate = mo2.week(mo2.week+1);
                var endDate = mo2.format("YYYY-MM-DD");
                this.startDate = startDate;
                this.endDate = endDate;

            },
            queryFlightPlane:function () {
                var url=basepath+"/api/flightPlane/queryScheded?startDate="+this.startDate+"&scheduled=true"
                    +"&endDate="+this.endDate+"&flightInfoId="+(this.flightInfoId?this.flightInfoId:"");
                this.url=url;
                var $this = this;
                axios.get(url).then(function (response) {
                    var a=[];
                    var resource=[];
                    response.data.forEach(function(data){

                        resource.push({id:data.flightPlane.planeId,title:data.flightPlane.flightInfo.flightNum})
                        data.groundServiceSchedulings.schedulings.forEach(function(schedule){
                            schedule.selectedUser.forEach(function (user) {
                                if(schedule.groundServiceItemPo.timeStrategy=="落地时间"){
                                    user.start = data.flightPlane.arriveTime;
                                    user.end = data.flightPlane.takeOffTime;
                                }
                                if(schedule.groundServiceItemPo.timeStrategy=="起飞时间"){
                                    user.start = data.flightPlane.takeOffTime;
                                    user.end = data.flightPlane.takeOffTime;
                                }
                                user.id=user.userId;
                                user.resourceId = data.flightPlane.planeId;
                               a.push(user);
                            })
                        })
                    })
                    this.schedule = a;
                    this.flightPlane = resource;
                })
            },
             loadJs:function () {
                var $this = this;
                 require.async("../../../lib/fullcalendar/moment.min.js",function() {
                     require.async("../../../lib/fullcalendar/fullcalendar.js", function () {
                         require.async("../../../lib/fullcalendar/locale/zh-cn.js", function () {
                             require.async("../../../lib/fullcalendar/scheduler.min.js", function () {
                                 $this.jsLoading=false;
                             })
                         })
                     })
                 })

              }

        },
        watch:{
            jsLoading:function(ov,nv){

                var a = moment();
                $('#calendar').fullCalendar({
                    now: a.format("YYYY-MM-DD"),
                    editable: false, // enable draggable events
                    aspectRatio: 1.8,
                    displayEventTime:true,
                    scrollTime: '00:00', // undo default 6am scrollTime
                    header: {
                        left: 'today prev,next',
                        center: 'title',
                        right: 'timelineDay,basicWeek,month'
                    },
                    defaultView: 'timelineDay',
                    views: {
                        // timelineDay:{
                        //     slotLabelInterval:true,
                        //     slotDuration:
                        // }
                    },
                    eventTextColor:"#333",
                    eventBackgroundColor:"rgba(0,0,0,0.02)",
                    slotLabelInterval:'02:00:00',
                    resourceLabelText: '航班',
                    resources:function(callback, start, end, timezone){
                        resourceCallback= callback;
                    },
                    events:function( start, end, timezone, callback ){
                        // console.log(start);
                        // console.log(end);

                        var startDate =start.format("YYYY-MM-DD");
                        var endDate = end.format("YYYY-MM-DD");
                        var url=basepath+"/api/flightPlane/queryScheded?startDate="+startDate+"&scheduled=true"
                            +"&endDate="+endDate+"&flightInfoId="//+($this.flightInfoId?$this.flightInfoId:"");
                        this.url=url;
                        var $this = this;
                        axios.get(url).then(function (response) {
                            var a=[];
                            var resource=[];
                            var resourcemap = {};
                            var index = 0;
                            response.data.forEach(function(eachOne){
                                var data = {};

                                data.flightPlane = eachOne[0];
                                var schedule=eachOne[1];
                                resourcemap[data.flightPlane.planeId]={id:data.flightPlane.planeId,title:data.flightPlane.flightInfo.flightNum,flightPlane:data.flightPlane};
                                schedule.selectedFwUser.forEach(function (user) {
                                    if(schedule.groundServiceItemPo.timeStrategy=="落地时间"){
                                        if(schedule.groundServiceItemPo.beforeOrAfter=='基准时间前'){
                                            user.start = new Date(new Date(data.flightPlane.arriveTime).getTime()-schedule.groundServiceItemPo.difference*60*1000);
                                        }else{
                                            user.start = new Date(new Date(data.flightPlane.arriveTime).getTime()+schedule.groundServiceItemPo.difference*60*1000);
                                        }


                                    }
                                    if(schedule.groundServiceItemPo.timeStrategy=="起飞时间"){
                                        if(schedule.groundServiceItemPo.beforeOrAfter=='基准时间前'){
                                            user.start = new Date(new Date(data.flightPlane.takeOffTime).getTime()-schedule.groundServiceItemPo.difference*60*1000);
                                        }else{
                                            user.start = new Date(new Date(data.flightPlane.takeOffTime).getTime()+schedule.groundServiceItemPo.difference*60*1000);
                                        }

                                    }
                                    user.groundServiceItemPo = schedule.groundServiceItemPo;
                                    user.id=index++;
                                    user.title = user.userCnName;
                                    user.resourceId = data.flightPlane.planeId;
                                    user.flightNum = data.flightPlane.flightInfo.flightNum;
                                    a.push(user);
                                })
                            })
                            for(var key in resourcemap){
                                resource.push(resourcemap[key]);
                            }
                            callback(a);
                            resourceCallback(resource);
                        });

                    },
                    resourceRender: function(resource, labelTds, bodyTds) {

                        labelTds.find(".fc-cell-content").html("<div>"+resource.title+"</div><div>"+resource.flightPlane.originating+
                            "-"+resource.flightPlane.destination+"</div>"+"<div>起飞："+moment(resource.flightPlane.takeOffTime).format("HH:mm")+"</div><div>落地："
                            +moment(resource.flightPlane.arriveTime).format("HH:mm")+"</div>")
                    }
                    ,eventRender: function(event, element) {
                        var type= $("#calendar").fullCalendar( 'getView' ).type;
                        var start=event.start.format('HH:mm:ss');
                        if(type=='timelineDay'){
                            // <div class=""><span class="fc-time">12:35</span><span class="fc-title" style="top: 0px; left: 0px;">张国亮</span></div>
                            element.html('<div style="margin-bottom: 10px;"><div class="fc-content"> <span class="fc-title">'+event.groundServiceItemPo.itemName+'</span></div>' +
                                '<div class="fc-content"><span class="fc-time">' +start+
                                '</span></div><div class="fc-content"> <span class="fc-title">'+event.title+'</span></div></div>'
                            );
                        }else{
                            element.html(
                                '<div style="margin-bottom: 10px;"><div class="fc-content">' +
                                '<span class="fc-title">'+event.flightNum+'</span>&nbsp;&nbsp;&nbsp;' +
                                '<span class="fc-title">'+event.groundServiceItemPo.itemName+'</span>' +

                                '</div>' +
                                '<div class="fc-content"><span class="fc-time">' +start+
                                '</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="fc-title">'+event.title+'</span></div></div>'
                            );
                        }


                    }
                });
            }
        },
        data:function () {


            return {
                startDate:"",
                endDate:"",
                flightInfoId:"",
                schedule:[],
                flightPlane:[],
                jsLoading:true
            };
        },
        mounted:function () {
            this.loadJs();
        }
        ,
        created:function (){

            var resourceCallback ;
            // require.async("../../../lib/fullcalendar/moment.min.js",function(){
            //     require.async("../../../lib/fullcalendar/fullcalendar.js",function(){
            //         require.async("../../../lib/fullcalendar/locale/zh-cn.js",function(){
            //             require.async("../../../lib/fullcalendar/scheduler.min.js",function(){
            //             //start
            //
            //             setTimeout(function () {
            //
            //
            //
            //
            //
            //
            //
            //
            //
            //
            //
            //
            //
            //             })
            //
            //
            //             //end
            //             });
            //         });
            //         });
            //     });

        },
        activated:function () {
            messages.$emit('change','地服排班表');
        }
    });
    module.exports = VueComponent;
    return VueComponent;
})